Merge pull request #1118 from purposeindustries/delayagent-maxemittedevents

Do not emit all the events on DelayAgent#check

Andrew Cantino 9 years ago
parent
commit
664188ca35
2 changed files with 26 additions and 3 deletions
  1. 11 3
      app/models/agents/delay_agent.rb
  2. 15 0
      spec/models/agents/delay_agent_spec.rb

+ 11 - 3
app/models/agents/delay_agent.rb

@@ -11,6 +11,8 @@ module Agents
11 11
 
12 12
       `expected_receive_period_in_days` is used to determine if the Agent is working. Set it to the maximum number of days
13 13
       that you anticipate passing without this Agent receiving an incoming Event.
14
+
15
+      `max_emitted_events` is used to limit the number of the maximum events which should be created. If you omit this DelayAgent will create events for every event stored in the memory.
14 16
     MD
15 17
 
16 18
     def default_options
@@ -55,11 +57,17 @@ module Agents
55 57
 
56 58
     def check
57 59
       if memory['event_ids'] && memory['event_ids'].length > 0
58
-        received_events.where(id: memory['event_ids']).reorder('events.id asc').each do |event|
60
+        events = received_events.where(id: memory['event_ids']).reorder('events.id asc')
61
+
62
+        if options['max_emitted_events'].present?
63
+          events = events.limit(options['max_emitted_events'].to_i)
64
+        end
65
+
66
+        events.each do |event|
59 67
           create_event payload: event.payload
68
+          memory['event_ids'].delete(event.id)
60 69
         end
61
-        memory['event_ids'] = []
62 70
       end
63 71
     end
64 72
   end
65
-end
73
+end

+ 15 - 0
spec/models/agents/delay_agent_spec.rb

@@ -108,5 +108,20 @@ describe Agents::DelayAgent do
108 108
 
109 109
       expect(agent.memory['event_ids']).to eq []
110 110
     end
111
+
112
+    it "re-emits max_emitted_events and clears just them from the memory" do
113
+      agent.options['max_emitted_events'] = 1
114
+      agent.receive([first_event, second_event, third_event])
115
+      expect(agent.memory['event_ids']).to eq [second_event.id, third_event.id]
116
+
117
+      expect {
118
+        agent.check
119
+      }.to change { agent.events.count }.by(1)
120
+
121
+      events = agent.events.reorder('events.id desc')
122
+      expect(agent.memory['event_ids']).to eq [third_event.id]
123
+      expect(events.first.payload).to eq second_event.payload
124
+
125
+    end
111 126
   end
112 127
 end